﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RM.Common;
using RM.DBUtility;
using System.Data;
using System.Data.SqlClient;
namespace RM.DAL
{
    public partial class RMUsers
    {
        public RMUsers()
        { }

        #region 基本方法
        /// <summary>
        /// 根据ID判断是否存在
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from X_Users");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
                                            new SqlParameter("@id",SqlDbType.Int)};
            parameters[0].Value = id;
            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 判断用户名或者姓名是否存在
        /// </summary>
        /// <param name="username">用户名或姓名</param>
        /// <returns></returns>
        public bool Exists(string username)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from X_Users");
            strSql.Append(" where username=@username");
            SqlParameter[] parameters ={
                                          new SqlParameter("@username",SqlDbType.VarChar)};
            parameters[0].Value = username;
            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 工资号是否存在
        /// </summary>
        /// <param name="salary">工资号</param>
        /// <param name="obj">占位，随便填什么都可以</param>
        /// <returns></returns>
        public bool Exists(string salary, object obj)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from X_Users");
            strSql.Append(" where salary=@salary");
            SqlParameter[] parameters ={
                                          new SqlParameter("@salary",SqlDbType.VarChar)
                                      };
            parameters[0].Value = salary;
            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 通过ID得到一个对象实体
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns></returns>
        public Model.RMUsers GetModel(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 id,username,password,isenable,gender,salary,identitycard,cellphone,remark");
            strSql.Append(" from X_Users");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters ={
                                          new SqlParameter("@id",SqlDbType.Int)
                                      };
            parameters[0].Value = id;
            Model.RMUsers model = new Model.RMUsers();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                model.UserName = ds.Tables[0].Rows[0]["username"].ToString();
                model.Password = ds.Tables[0].Rows[0]["password"].ToString();
                model.Gender = ds.Tables[0].Rows[0]["gender"].ToString();
                model.Salary = ds.Tables[0].Rows[0]["salary"].ToString();
                model.IsEnable = bool.Parse(ds.Tables[0].Rows[0]["isenable"].ToString());
                model.IdentityCard = ds.Tables[0].Rows[0]["identity"].ToString();
                model.CellPhone = ds.Tables[0].Rows[0]["cellphone"].ToString();
                model.Remark = ds.Tables[0].Rows[0]["remark"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 通过用户名（姓名）获得一个对象实体
        /// </summary>
        /// <param name="username">用户名（姓名）</param>
        /// <returns></returns>
        public Model.RMUsers GetModel(string username)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 id,username,password,isenable,gender,salary,identitycard,cellphone,remark");
            strSql.Append(" from X_Users");
            strSql.Append(" where username=@username");
            SqlParameter[] parameters ={
                                          new SqlParameter("@username",SqlDbType.VarChar,50)
                                      };
            parameters[0].Value = username;
            Model.RMUsers model = new Model.RMUsers();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                model.UserName = ds.Tables[0].Rows[0]["username"].ToString();
                model.Password = ds.Tables[0].Rows[0]["password"].ToString();
                model.Gender = ds.Tables[0].Rows[0]["gender"].ToString();
                model.IsEnable = bool.Parse(ds.Tables[0].Rows[0]["isenable"].ToString());
                model.IdentityCard = ds.Tables[0].Rows[0]["identitycard"].ToString();
                model.Salary = ds.Tables[0].Rows[0]["salary"].ToString();
                model.Remark = ds.Tables[0].Rows[0]["remark"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 通过工资号获得一个对象实体
        /// </summary>
        /// <param name="salary">工资号</param>
        /// <param name="obj">占位，留空，没啥作用</param>
        /// <returns></returns>
        public Model.RMUsers GetModel(string salary, object obj)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 id,username,password,isenable,gender,salary,identitycard,cellphone,remark");
            strSql.Append(" from X_Users");
            strSql.Append(" where salary=@salary");
            SqlParameter[] parameters ={
                                          new SqlParameter("@salary",SqlDbType.VarChar,50)
                                      };
            parameters[0].Value = salary;
            Model.RMUsers model = new Model.RMUsers();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                model.UserName = ds.Tables[0].Rows[0]["username"].ToString();
                model.Password = ds.Tables[0].Rows[0]["password"].ToString();
                model.Gender = ds.Tables[0].Rows[0]["gender"].ToString();
                model.IsEnable = bool.Parse(ds.Tables[0].Rows[0]["isenable"].ToString());
                model.IdentityCard = ds.Tables[0].Rows[0]["identitycard"].ToString();
                model.Salary = ds.Tables[0].Rows[0]["salary"].ToString();
                model.Remark = ds.Tables[0].Rows[0]["remark"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 得到登录实体
        /// </summary>
        /// <param name="username"></param>
        /// <param name="salary"></param>
        /// <returns></returns>
        public Model.RMUsers GetLoginModel(string username, string salary)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 id,username,password,isenable,gender,salary,identitycard,cellphone,remark");
            strSql.Append(" from X_Users");
            strSql.Append(" where username=@username or salary=@salary");
            SqlParameter[] parameters = {
                                            new SqlParameter("@username",SqlDbType.VarChar,50),
                                            new SqlParameter("@salary",SqlDbType.VarChar,50)
                                        };
            parameters[0].Value = username;
            parameters[1].Value = salary;
            Model.RMUsers model = new Model.RMUsers();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                model.UserName = ds.Tables[0].Rows[0]["username"].ToString();
                model.Password = ds.Tables[0].Rows[0]["password"].ToString();
                model.IsEnable = bool.Parse(ds.Tables[0].Rows[0]["isenable"].ToString());
                model.Gender = ds.Tables[0].Rows[0]["gender"].ToString();
                model.Salary = ds.Tables[0].Rows[0]["salary"].ToString();
                model.IdentityCard = ds.Tables[0].Rows[0]["identitycard"].ToString();
                model.CellPhone = ds.Tables[0].Rows[0]["cellphone"].ToString();
                model.Remark = ds.Tables[0].Rows[0]["remark"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 获取查询分页数据
        /// </summary>
        public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from X_Users");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            recordCount = Convert.ToInt32(DbHelperSQL.GetSingle(PagingHelper.CreateCountingSql(strSql.ToString())));
            return DbHelperSQL.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, strSql.ToString(), filedOrder));
        }


        #endregion
    }
}
